﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using LiSAClient.Core;
using LiSAClient.Core.Interfaces;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;
using LiSAClient.LiSAService;

namespace LiSAClient.WpfClient.ViewModel.Projects.Edit
{
    class EditCriteriaViewModel : ContentViewModel, ICommitable
    {
        public ICommand Commit { get; private set; }
        public ICommand AddCriteria { get; private set; }
        
        private Project project;
        public Project Project
        {
            get
            {
                return this.project;
            }

            set
            {
                this.Set(() => this.Project, ref this.project, value);
            }
        }

        private ProjectCriteria newCriteria;
        public ProjectCriteria NewCriteria
        {
            get
            {
                return this.newCriteria;
            }

            set
            {
                this.Set(() => this.NewCriteria, ref this.newCriteria, value);
            }
        }

        private ProjectCriteria selectedCriteria;
        public ProjectCriteria SelectedCriteria
        {
            get
            {
                return this.selectedCriteria;
            }

            set
            {
                this.Set(() => this.SelectedCriteria, ref this.selectedCriteria, value);
            }
        }

        #region Initializers

        public EditCriteriaViewModel(Project proj)
        {
            this.Project = proj;
            this.NewCriteria = new ProjectCriteria();
            this.Commit = new RelayCommand(SaveProject);
            this.AddCriteria = new RelayCommand(DoAddCriteria);
        }
        #endregion

        #region methods
        public void DoAddCriteria()
        {
            if (string.IsNullOrWhiteSpace(NewCriteria.CriteriaID) || string.IsNullOrWhiteSpace(NewCriteria.Description))
            {
                return;
            }
            this.NewCriteria.ID = Guid.NewGuid();
            var newCriteriaList = new List<ProjectCriteria>(this.Project.Criteria) {NewCriteria}; 
            this.Project.Criteria = newCriteriaList;
            this.NewCriteria = new ProjectCriteria {CriteriaID = string.Empty, Description = string.Empty};
        }

        /// <summary>
        /// Save the project and close the window
        /// </summary>
        public void SaveProject()
        {
            MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Update Project?", "Update Confirmation", System.Windows.MessageBoxButton.YesNo);
            if (messageBoxResult == MessageBoxResult.Yes)
            {
                ServiceFactory.Instance.Get<ProjectService>().UpdateProject(this.Project);

                // Close the window
                if (this.Presenter != null)
                {
                    this.Presenter.SendClosingRequest();
                }
            }
        }
        #endregion
    }
}
